package 第三章作业;

import java.io.*;

public class Work1 {
    public void create(File file) throws IOException{
        if (!file.exists()){
            file.createNewFile();
        }
    }
    public static void main(String[] args) {
        Work1 w=new Work1();
       File file=new File("F:\\java_senior_collection\\doc\\test.txt");
        try {
            w.create(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("F:\\java_senior_collection\\doc\\test.txt",true);
            String str="hello world";
           byte[] words=str.getBytes();
            fos.write(words,0,words.length);
            System.out.println("写入hello world 已完成");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileInputStream fis=null;
        try {
            fis=new FileInputStream("F:\\java_senior_collection\\doc\\test.txt");
            int data;
            while((data=fis.read())!=-1){
                System.out.print(data+" ");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
